package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.*;
import com.the4man.cnbdw.utils.Page;
import com.the4man.cnbdw.utils.SelectModel;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.*;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Action(
        value = "levelAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/level/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/level/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/level/edit.jsp"
        )}
)
public class LevelAction extends BaseAction implements ModelDriven<Level> {
    private Level level = new Level();
    private LogInfo logInfo = new LogInfo();
    @Autowired
    private LogInfoService logService;
    @Autowired
    private LevelService levelService;
    @Autowired
    private LevelRegionMapService levelRegionMapService;
    @Autowired
    private LevelRoomTypeMapService levelRoomTypeMapService;
    @Autowired
    private RegionInfoService regionInfoService;

    public LevelAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        return "addPage";
    }

    public String toEditPage() {
        try {
            Level d = (Level)this.levelService.selectByPrimaryKey(this.getRequest().getParameter("id"), Level.class);
            BeanUtils.copyProperties(d, this.level);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            LevelExample levelExample = new LevelExample();
            if(StringUtils.isNotEmpty(this.level.getName())) {
                levelExample.createCriteria().andNameLike("%" + this.level.getName() + "%");
            }

            int total = this.levelService.countByExample(levelExample);
            Page<Level> page = new Page();
            this.setPageProperties(page);
            this.levelService.selectByExample(levelExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal(Long.valueOf((long)total));
            this.writeJson(d);
        } catch (Exception var5) {
            LOG.error("获取数据失败", var5, new String[0]);
        }

    }

    public void add() {
        PageJson json = new PageJson();

        try {
            String name = this.getRequest().getParameter("name");
            Level lev = this.levelService.selectByName(name);
            if(lev == null) {
                String time = DateUtil.getInstance().formatDate(new Date());
                this.level.setId(UUID.randomUUID().toString());
                this.level.setCreateTime(time);
                this.level.setUpdateTime(time);
                this.level.setCreateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.level.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.levelService.insert(this.level);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(time);
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("人员级别管理");
                this.logInfo.setOp("人员级别添加");
                this.logInfo.setMemo("人员级别:" + name);
                this.logService.insert(this.logInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("级别名称已存在");
            }
        } catch (Exception var5) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var5, new String[0]);
        }

        this.writeJson(json);
    }

    public void update() {
        PageJson json = new PageJson();

        try {
            String id = this.level.getId();
            String name = this.level.getName();
            Level lev = this.levelService.selectMByName(id, name);
            if(lev == null) {
                this.level.setUpdateTime(DateUtil.getInstance().formatDate(new Date()));
                this.level.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.levelService.updateByPrimaryKey(this.level);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("人员级别管理");
                this.logInfo.setOp("人员级别修改");
                this.logInfo.setMemo("人员级别:" + this.level.getName());
                this.logService.insert(this.logInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("级别名称已存在");
            }
        } catch (Exception var5) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var5, new String[0]);
        }

        this.writeJson(json);
    }

    public String executeModifyResult() {
        try {
            Object instance = this.levelService.selectByPrimaryKey(this.level.getId(), Level.class);
            this.getRequest().setAttribute("instance", instance);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

        return "modifyPage";
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int count = 0;
            String ids = this.getRequest().getParameter("ids");
            if(ids.contains(",")) {
                String[] str = ids.split(",");
                String[] var8 = str;
                int var7 = str.length;

                for(int var16 = 0; var16 < var7; ++var16) {
                    String info = var8[var16];
                    Level d = (Level)this.levelService.selectByPrimaryKey(info, Level.class);
                    int i = this.levelService.deleteByPrimaryKey(info, Level.class);
                    LevelRegionMapExample example = new LevelRegionMapExample();
                    example.createCriteria().andLidEqualTo(info);
                    this.levelRegionMapService.deleteByExample(example);
                    LevelRoomTypeMapExample example2 = new LevelRoomTypeMapExample();
                    example.createCriteria().andLidEqualTo(info);
                    this.levelRoomTypeMapService.deleteByExample(example2);
                    if(i > 0) {
                        this.logInfo.setId(UUID.randomUUID().toString());
                        this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                        this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                        this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                        this.logInfo.setFunc("人员级别管理");
                        this.logInfo.setOp("人员级别批量删除");
                        this.logInfo.setMemo("人员级别:" + d.getName());
                        this.logService.insert(this.logInfo);
                        ++count;
                    }
                }
            } else {
                Level d = (Level)this.levelService.selectByPrimaryKey(this.getRequest().getParameter("ids"), Level.class);
                count = this.levelService.deleteByPrimaryKey(this.getRequest().getParameter("ids"), Level.class);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("人员级别管理");
                this.logInfo.setOp("人员级别删除");
                this.logInfo.setMemo("人员级别:" + d.getName());
                this.logService.insert(this.logInfo);
                LevelRegionMapExample example = new LevelRegionMapExample();
                example.createCriteria().andLidEqualTo(this.getRequest().getParameter("ids"));
                this.levelRegionMapService.deleteByExample(example);
                LevelRoomTypeMapExample example2 = new LevelRoomTypeMapExample();
                example.createCriteria().andLidEqualTo(this.getRequest().getParameter("ids"));
                this.levelRoomTypeMapService.deleteByExample(example2);
            }

            if(count > 0) {
                json.setReturnMsg("成功删除" + count + "个级别！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除级别失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var13) {
            LOG.error("批量删除级别失败！", var13, new String[0]);
            json.setReturnMsg("删除级别失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void getRegionRelation() {
        DataGrid d = new DataGrid();

        try {
            LevelRegionMapExample levelRegionMapExample = new LevelRegionMapExample();
            levelRegionMapExample.createCriteria().andLidEqualTo(this.getRequest().getParameter("levelId"));
            List<LevelRegionMap> list = this.levelRegionMapService.selectByExample(levelRegionMapExample);
            if(list != null && !list.isEmpty()) {
                List<String> regionIds = new ArrayList();
                Iterator var6 = list.iterator();

                while(var6.hasNext()) {
                    LevelRegionMap levelRegionMap = (LevelRegionMap)var6.next();
                    regionIds.add(levelRegionMap.getRid());
                }

                RegionInfoExample example = new RegionInfoExample();
                example.createCriteria().andIdIn(regionIds);
                List regions = this.regionInfoService.selectByExample(example);
                d.setRows(regions);
                d.setTotal(Long.valueOf((long)regions.size()));
            }
        } catch (Exception var7) {
            LOG.error("批量删除级别失败！", var7, new String[0]);
        }

        this.writeJson(d);
    }

    public void regionRelation() {
        PageJson json = new PageJson();

        try {
            int count = 0;
            String ids = this.getRequest().getParameter("ids");
            if(StringUtils.isNotEmpty(this.level.getId()) && StringUtils.isNotEmpty(ids)) {
                LevelRegionMapExample example = new LevelRegionMapExample();
                example.createCriteria().andLidEqualTo(this.level.getId());
                this.levelRegionMapService.deleteByExample(example);
                String[] temp = ids.split(",");
                String[] var9 = temp;
                int var8 = temp.length;

                for(int var7 = 0; var7 < var8; ++var7) {
                    String info = var9[var7];
                    LevelRegionMap lr = new LevelRegionMap();
                    lr.setId(UUID.randomUUID().toString());
                    lr.setLid(this.level.getId());
                    lr.setRid(info);
                    lr.setCreateTime(new Date());
                    lr.setCreateUser(RequestUtil.getSessionUserinfo().getUsername());
                    int i = this.levelRegionMapService.insert(lr);
                    if(i > 0) {
                        ++count;
                    }
                }

                json.setReturnMsg("成功关联" + count + "个区域");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("参数为空");
                json.setReturnFlag(false);
            }
        } catch (Exception var12) {
            LOG.error("关联区域失败！", var12, new String[0]);
            json.setReturnMsg("关联区域失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void roomTypeRelation() {
        PageJson json = new PageJson();

        try {
            int count = 0;
            String ids = this.getRequest().getParameter("ids");
            if(StringUtils.isNotEmpty(this.level.getId()) && StringUtils.isNotEmpty(ids)) {
                LevelRoomTypeMapExample example = new LevelRoomTypeMapExample();
                example.createCriteria().andLidEqualTo(this.level.getId());
                this.levelRoomTypeMapService.deleteByExample(example);
                String[] temp = ids.split(",");
                String[] var9 = temp;
                int var8 = temp.length;

                for(int var7 = 0; var7 < var8; ++var7) {
                    String info = var9[var7];
                    LevelRoomTypeMap lr = new LevelRoomTypeMap();
                    lr.setId(UUID.randomUUID().toString());
                    lr.setLid(this.level.getId());
                    lr.setRtid(info);
                    lr.setCreateTime(new Date());
                    lr.setCreateUser(RequestUtil.getSessionUserinfo().getUsername());
                    int i = this.levelRoomTypeMapService.insert(lr);
                    if(i > 0) {
                        ++count;
                    }
                }

                json.setReturnMsg("成功关联" + count + "个舱室类别");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("参数为空");
                json.setReturnFlag(false);
            }
        } catch (Exception var12) {
            LOG.error("关联区域失败！", var12, new String[0]);
            json.setReturnMsg("关联区域失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void verify() {
        PageJson json = new PageJson();
        this.writeJson(json);
    }

    public void getLevelSelectData() {
        try {
            List<SelectModel> list = this.levelService.getLevelSelectData();
            this.writeJson(list);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public Level getModel() {
        return this.level;
    }
}
